chrome.storage.local.get('state').then(res => {
	if (!res.state) {
		return;
	}

	doInternalRules();

	doUserRules();
});

function doInternalRules() {
	console.log('开始内置广告删除')
	// 网易ad
	$("div[adtype]").remove();

	doBaiduEasyleanRules();
}

function doBaiduEasyleanRules() {

	if (location.href.indexOf('easylearn.baidu.com') < 0) {
		return;
	}

	setTimeout(function() {
		console.log('开始百度题库自动打开答案解析')
		var btn = $(".toogle-btn  .more-text");
		if (btn) {
			btn.click();
			setTimeout(function() {
				$(".exercise-new-dialog .exercise-btn.exercise-btn-4").click();
			}, 500);
		}
	}, 1000);
}

function doUserRules() {
	chrome.storage.local.get('rules').then(rs => {

		if (!rs.rules) {
			return;
		}

		console.log('开始自定义规则处理：' + JSON.stringify(rs.rules));

		var url = location.href;

		for (var rule of rs.rules) {
			if (url.indexOf(rule.url) < 0) {
				continue;
			}

			for (var elem of rule.elems) {
				$(elem).remove();
			}
		}

	});
}